﻿using System;
using System.Collections.Generic;
using Excepciones;

namespace LisBioGUIWPF.util
{

    [LisBioException]
    public class Util
    {

        private static string Prefix = "";

        public static DateTime convertTextToDate(string text)
        {
            DateTime date;

            char[] separator = { '/' };
            string[] dateParts = text.Split(separator);

            int year = int.Parse(dateParts[2]);
            int month = int.Parse(dateParts[1]);
            int day = int.Parse(dateParts[0]);

            date = new DateTime(year, month, day);

            return date;
        }

        public static string obtainImageURL(string pathFile, string @targetPath)
        {
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            char[] separator = { '\\' };
            string[] nameArray = pathFile.Split(separator);
            string fileName = nameArray[nameArray.Length - 1];

            //Usa la clase path para manipular los paths de los directorios o archivos.
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            //Copia el archivo y lo sobreescribe si existe.
            if (!System.IO.File.Exists(destFile))
            {
                System.IO.File.Copy(pathFile, destFile, true);
            }

            return destFile;
        }

        private static bool StartWithPrefix(string s)
        {
            if (Util.Prefix.ToLower().Equals(""))
            {
                return true;
            }
            else if ( s.ToLower().StartsWith(Util.Prefix.ToLower()) )
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void FilterCombo(System.Windows.Controls.ComboBox combo, List<string> list)
        {
            Util.Prefix = combo.Text;

            List<string> listFind = list.FindAll(Util.StartWithPrefix);
            combo.ItemsSource = listFind;

        }


    }
}
